import dayjs from 'dayjs'
import NP from 'number-precision'

const filters = {
  formatDate(value, format) {
    if (value) {
      return dayjs(value).format(format || 'YYYY-MM-DD HH:mm:ss')
    } else {
      return ''
    }
  },
  formatDay(value, format) {
    if (value) {
      return dayjs(value).format(format || 'YYYY-MM-DD')
    } else {
      return ''
    }
  },
  formatSeconds(value) {
    let hourStr = '',
      minStr = '',
      secStr = '',
      totalSeconds = value
    // 计算小时数
    const totalHours = Math.floor(totalSeconds / (60 * 60))
    // 从秒数中扣除小时
    totalSeconds = totalSeconds - (totalHours * 60 * 60)
    // 计算剩余的分钟个数
    const totalMinutes = Math.floor(totalSeconds / 60)
    // 获得余下的秒数
    totalSeconds = totalSeconds - (totalMinutes * 60)
    // 日期格式赋值
    hourStr = totalHours.toString().length === 1 ? '0' + totalHours.toString() : totalHours.toString()
    minStr = totalMinutes.toString().length === 1 ? '0' + totalMinutes.toString() : totalMinutes.toString()
    secStr = totalSeconds.toString().length === 1 ? '0' + totalSeconds.toString() : totalSeconds.toString()
    return `${hourStr}:${minStr}:${secStr}`
  },
  price(value) {
    if (value) {
      return NP.divide(value, 100)
    } else {
      return 0
    }
  }
}

export default filters
